﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Xml.Serialization;

namespace nGSO
{
    public class ExtendedAgentJump : BaseAfterAgentMoveExtension
    {
        private static Random _random = new Random();

        public override string Name
        {
            get { return "ExtendedAgentJumpIfNoNeighbours"; }
        }

        public override void Apply(Agent agent, GsoConfig config, ObjectiveFunction fitnessFun)
        {
            if (agent.Neighbors.Count < 1)
            {
                double offset = (0.5 - _random.NextDouble()) * config.StepSize;

                double[] nextLoc = new double[config.Dims];
                for (int i = 0; i < config.Dims; i++)
                {
                    nextLoc[i] = agent.Loc[i] + offset;
                }

                // dont change location if new agent fitness is worst than current
                double currentFitness = agent.Fitness;
                double potentialFitness = fitnessFun(agent, agent.Loc);

                if (potentialFitness > currentFitness)
                {
                    agent.NextLoc = nextLoc;
                    agent.History.Last().AppliedGsoV1 = true;
                }
            }
        }
    }
}
